package xyz.yizhifeimao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.yizhifeimao.common.ServiceResponse;
import xyz.yizhifeimao.dao.ShippingMapper;
import xyz.yizhifeimao.pojo.Shipping;
import xyz.yizhifeimao.service.IShippingService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：xuelibing
 * @date ：Created in 2020/11/27 15:49
 * @description：收货地址管理Service
 * @modified By：
 * @version: V1.0$
 */
@Service("iShippingService")
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    /**
     * 新建收货地址
     * 此处用到了Mybatis自动生成主键，并在ShippingMapper.xml中配置useGeneratedKeys="true"
     * keyProperty="id"两属性，调用insert后，自动将生成的主键赋值给shipping对象
     * @param userId
     * @param shipping
     * @return
     */
    public ServiceResponse add(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int rowCount = shippingMapper.insert(shipping);
        if (rowCount > 0) {
            Map map = new HashMap<String, Integer>();
            map.put("shippingId", shipping.getId());
            return ServiceResponse.createBySuccess("新建地址成功", map);
        }
        return ServiceResponse.createByErrorMessage("新建地址失败");
    }

    /**
     * 根据shippingId、userId删除收回地址
     * @param userId
     * @param shippingId
     * @return
     */
    public ServiceResponse<String> del(Integer userId, Integer shippingId) {
        int rowCount = shippingMapper.deleteByUserIdShippingId(userId, shippingId);
        if (rowCount > 0) {
            return ServiceResponse.createBySuccess("删除地址成功");
        }
        return ServiceResponse.createByErrorMessage("删除地址失败");
    }

    /**
     * 更新收货地址
     * @param userId
     * @param shipping
     * @return
     */
    public ServiceResponse update(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateByShipping(shipping);
        if (rowCount > 0) {
            return ServiceResponse.createBySuccess("更新地址成功");
        }
        return ServiceResponse.createByErrorMessage("更新地址失败");
    }

    /**
     * 查询收货地址
     * @param userId
     * @param shippingId
     * @return
     */
    public ServiceResponse<Shipping> select(Integer userId, Integer shippingId) {
        Shipping shipping = shippingMapper.selectByUserIdShippingId(userId, shippingId);
        if (shipping == null) {
            return ServiceResponse.createByErrorMessage("未查到该地址");
        }
        return ServiceResponse.createBySuccess("查询地址成功", shipping);
    }

    /**
     * 收货地址分页查询
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ServiceResponse<PageInfo> list(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shipping> shippingList = shippingMapper.selectShippingByUserId(userId);
        PageInfo pageInfo = new PageInfo(shippingList);
        return ServiceResponse.createBySuccess(pageInfo);
    }


}
